package com.hliushi.learn.job;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.hliushi.learn.model.FileCustom;
import lombok.extern.slf4j.Slf4j;

/**
 * 启动任务
 *
 * @author llhuang10
 * @date 2021/8/16 22:09
 */
@Slf4j
public class JobMain {


    /**
     * zookeeper端口
     */
    private static final int ZOOKEEPER_PORT = 2181;

    /**
     * zookeeper链接字符串 localhost:2181
     */
    private static final String ZOOKEEPER_CONNECTION_STRING = "cvm-node01:" + ZOOKEEPER_PORT;
    /**
     * 定时任务命名空间
     */
    private static final String JOB_NAMESPACE = "elastic-job-example-java";


    /**
     * 执行启动任务
     */
    public static void main(String[] args) {
        // 创造一些测试数据
        generateTestFiles();
        // 配置注册中心
        CoordinatorRegistryCenter registryCenter = setUpRegistryCenter();
        // 启动任务
        startJob(registryCenter);
    }


    /**
     * zk的配置及创建任务中心
     *
     * @return 用于协调分布式服务的注册中心
     */
    public static CoordinatorRegistryCenter setUpRegistryCenter() {
        // zk的配置
        ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(ZOOKEEPER_CONNECTION_STRING, JOB_NAMESPACE);
        // 减少zk的超时时间
        zookeeperConfiguration.setSessionTimeoutMilliseconds(100);
        // 创建注册中心
        ZookeeperRegistryCenter registryCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
        registryCenter.init();
        return registryCenter;
    }

    /**
     * 配置并启动任务
     *
     * @param registryCenter 注册中心
     */
    public static void startJob(CoordinatorRegistryCenter registryCenter) {
        // 创建JobCoreConfiguration
        /**
         * JobCoreConfiguration.newBuilder(val1, val2, val3)
         * #
         * 创建简单作业配置构建器.
         *
         * @param jobName 作业名称
         * @param cron 作业启动时间的cron表达式
         * @param shardingTotalCount 作业分片总数
         * @return 简单作业配置构建器
         */
        JobCoreConfiguration jobCoreConfiguration =
                JobCoreConfiguration.newBuilder("files-job", "0/3 * * * * ?", 3).build();

        // 创建SimpleJobConfiguration
        // class.getCanonicalName() -->全限定类名
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, FileBackupJob.class.getCanonicalName());

        // 创建JobScheduler 启动任务
        new JobScheduler(registryCenter,
                LiteJobConfiguration.newBuilder(simpleJobConfiguration).overwrite(true).build()).init();
    }


    /**
     * 生成测试文件
     */
    private static void generateTestFiles() {
        for (int i = 1; i < 11; i++) {
            FileBackupJob.files.add(new FileCustom(String.valueOf(i + 10), "文件" + (i + 10), "text", "content" + (i + 10)));
            FileBackupJob.files.add(new FileCustom(String.valueOf(i + 20), "文件" + (i + 20), "image", "content" + (i + 20)));
            FileBackupJob.files.add(new FileCustom(String.valueOf(i + 30), "文件" + (i + 30), "radio", "content" + (i + 30)));
            FileBackupJob.files.add(new FileCustom(String.valueOf(i + 40), "文件" + (i + 40), "vedio", "content" + (i + 40)));
        }
        log.info("data init success!!!");

        FileBackupJob.files.stream().forEach(System.out::println);
    }
}
